/*
 * Managed bean, which creates new articles and shows all articles
 *
 */
package edu.city.set.eia.cityblog.entities.managedbeans;

import edu.city.set.eia.cityblog.entities.Article;
import edu.city.set.eia.cityblog.sessionbeans.ArticleFacadeLocal;
import javax.faces.bean.ManagedBean;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.ejb.EJB;
import java.util.List;
import java.util.Date;
import java.util.ResourceBundle;
import javax.annotation.PostConstruct;
import javax.faces.bean.SessionScoped;
/**
 *
 * @author user
 */
@ManagedBean
@SessionScoped
public class Articles {
    private static final ResourceBundle cityblog = ResourceBundle.getBundle("cityblog");
   
    /** Creates a new instance of Articles */
    public Articles() {
    }//end empty constructor
   
    //attributes
    List<Article> articles;
    Article selectedArticle;
    @EJB ArticleFacadeLocal articleFacade;
   
   
     @PostConstruct
    public void showArticless(){
        this.setArticles(articleFacade.findAll());
    }
   
    //set and get methods
    public void setArticles(List<Article> articles){
        this.articles = articles;
    }
    public List<Article> getArticle(){
        return this.articles;
    }
   
    public void setSelectedArticle( Article selArticle){
        this.selectedArticle = selArticle;
    }
    public Article getSelectedArticle(){
        return this.selectedArticle;
    }// end set and get methods
   
    //show articles
    public String showSelectedArticle(Integer articleId){
        selectedArticle = articleFacade.find(articleId);
        return "showArticle";
    }//end method
   
   
   
     public void persistSelectedArticle(){
        if (selectedArticle != null){
            articleFacade.edit(selectedArticle);
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_INFO,
                    cityblog.getString("update.message"), cityblog.getString("update.message")));
        }
    }
   
   
    //create new article
    public String newArticle(){
        Date date = new Date(1111111111);
        selectedArticle = new Article( 1,  "draft", date,  "crap","not normal",  "asrgbaslghas");
        return "showArticle";
    }//end method
   

}

